import os
import sys

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget

from ..config import ExcelConfig, R
from ..core.excel import ExcelAccount, ExcelFillColumn, ExcelFree, ExcelSale
from .fill_columns_ui import Ui_Form


class FillColumnsWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton_SelectOrgFile.clicked.connect(self.SelectOrgFile)
        self.ui.pushButton_SelectDestFile.clicked.connect(self.SelectDestFile)
        self.ui.pushButton_FillColumns_sum.clicked.connect(self.FillColumnsSum)
        self.ui.pushButton_FillColumn.clicked.connect(self.FillColumn)
        self.ui.pushButton_FillColumns_hz.clicked.connect(self.FillColumnsHZ)
        self.ui.pushButton_FillColumns_xt.clicked.connect(self.FillColumnsXT)
        self.InitUi()
        self.filename = os.path.join(os.path.expanduser('~'), 'Desktop/temp.txt') #"."

    def InitUi(self):
        self.ui.lineEdit_OrgFilename.setText(R.Org_filename)
        self.ui.lineEdit_OrgSheetName.setText(R.Org_sheetname)
        self.ui.lineEdit_OrgSheetClientId.setText(R.Org_client_id)
        self.ui.lineEdit_OrgSheetCodeCol.setText(R.Org_code_col)
        self.ui.lineEdit_OrgSheetStartRow.setText(R.Org_start_row)
        self.ui.lineEdit_OrgSheetEndRow.setText(R.Org_end_row)
        self.ui.lineEdit_OrgSheetStartCol.setText(R.Org_start_col)
        self.ui.lineEdit_OrgSheetEndCol.setText(R.Org_end_col)
        self.ui.lineEdit_DestFilename.setText(R.Dest_filename)
        self.ui.lineEdit_DestSheetName.setText(R.Dest_sheetname)
        self.ui.lineEdit_DestSheetClientRow.setText(R.Dest_client_row)
        self.ui.lineEdit_DestSheetCodeCol.setText(R.Dest_code_col)
        self.ui.lineEdit_DestSheetStartRow.setText(R.Dest_start_row)
        self.ui.lineEdit_DestSheetEndRow.setText(R.Dest_end_row)
        self.ui.lineEdit_DestSheetStartCol.setText(R.Dest_start_col)
        self.ui.lineEdit_DestSheetEndCol.setText(R.Dest_end_col)
        self.ui.lineEdit_DestSheetPriceCol.setText(R.Dest_price_col)
        self.ui.lineEdit_DestSheetSumRow.setText(R.Dest_sum_row)
        self.ui.checkBox_IsSum.setChecked(R.Dest_is_sum)
        self.ui.checkBox_isSort.setChecked(R.Dest_is_sort)

    def SelectOrgFile(self):
        filename, ext = QFileDialog.getOpenFileName(
            self,
            "OpenFile",
            os.path.dirname(self.filename),
            "Excel File (*.xlsx);; All File (*.*)",
        )
        if filename:
            self.ui.lineEdit_OrgFilename.setText(filename)
            self.filename = filename
        else:
            QMessageBox.about(self, "Open file", "No opened file!")

    def SelectDestFile(self):
        filename, ext = QFileDialog.getOpenFileName(
            self,
            "OpenFile",
            os.path.dirname(self.filename),
            "Excel File (*.xlsx);; All File (*.*)",
        )
        if filename:
            self.ui.lineEdit_DestFilename.setText(filename)
            self.filename = filename
        else:
            QMessageBox.about(self, "Open file", "No opened file!")

    def FillColumns(self, is_hz=True):
        try:
            org_filename = self.ui.lineEdit_OrgFilename.text()
            org_sheetname = self.ui.lineEdit_OrgSheetName.text()
            org_clientId = int(self.ui.lineEdit_OrgSheetClientId.text())
            org_codeCol = int(self.ui.lineEdit_OrgSheetCodeCol.text())
            org_startRow = int(self.ui.lineEdit_OrgSheetStartRow.text())
            org_endRow = int(self.ui.lineEdit_OrgSheetEndRow.text())
            org_startCol = int(self.ui.lineEdit_OrgSheetStartCol.text())
            org_endCol = int(self.ui.lineEdit_OrgSheetEndCol.text())
            dest_filename = self.ui.lineEdit_DestFilename.text()
            dest_sheetname = self.ui.lineEdit_DestSheetName.text()
            dest_clientRow = int(self.ui.lineEdit_DestSheetClientRow.text())
            dest_codeCol = int(self.ui.lineEdit_DestSheetCodeCol.text())
            dest_startRow = int(self.ui.lineEdit_DestSheetStartRow.text())
            dest_endRow = int(self.ui.lineEdit_DestSheetEndRow.text())
            dest_startCol = int(self.ui.lineEdit_DestSheetStartCol.text())
            dest_endCol = int(self.ui.lineEdit_DestSheetEndCol.text())
            dest_isSum = self.ui.checkBox_IsSum.isChecked()
            is_sort = self.ui.checkBox_isSort.isChecked()

            """ 根据代码，客户ID， 填充汇总列 """
            exl = ExcelFillColumn()
            if is_hz:
                exl.fill_all_hz(
                    src_filename=org_filename,
                    src_sheetname=org_sheetname,
                    src_client_id=org_clientId,
                    src_code_col=org_codeCol,
                    src_start_row=org_startRow,
                    src_end_row=org_endRow,
                    src_start_col=org_startCol,
                    src_end_col=org_endCol,
                    dest_filename=dest_filename,
                    dest_sheetname=dest_sheetname,
                    dest_client_row=dest_clientRow,
                    dest_code_col=dest_codeCol,
                    dest_start_row=dest_startRow,
                    dest_end_row=dest_endRow,
                    dest_start_col=dest_startCol,
                    dest_end_col=dest_endCol,
                    is_sum=dest_isSum,
                )
            else:
                exl.fill_all_xt(
                    src_filename=org_filename,
                    src_sheetname=org_sheetname,
                    src_client_id=org_clientId,
                    src_code_col=org_codeCol,
                    src_start_row=org_startRow,
                    src_end_row=org_endRow,
                    src_start_col=org_startCol,
                    src_end_col=org_endCol,
                    dest_filename=dest_filename,
                    dest_sheetname=dest_sheetname,
                    dest_client_row=dest_clientRow,
                    dest_code_col=dest_codeCol,
                    dest_start_row=dest_startRow,
                    dest_end_row=dest_endRow,
                    dest_start_col=dest_startCol,
                    dest_end_col=dest_endCol,
                    is_sort=is_sort
                )

            QMessageBox.about(self, R.AppName, "填充列成功！")

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")

    def FillColumnsSum(self):
        try:
            dest_filename = self.ui.lineEdit_DestFilename.text()
            dest_sheetname = self.ui.lineEdit_DestSheetName.text()
            dest_sumRow = int(self.ui.lineEdit_DestSheetSumRow.text())
            dest_priceCol = int(self.ui.lineEdit_DestSheetPriceCol.text())
            dest_startRow = int(self.ui.lineEdit_DestSheetStartRow.text())
            dest_endRow = int(self.ui.lineEdit_DestSheetEndRow.text())
            dest_startCol = int(self.ui.lineEdit_DestSheetStartCol.text())
            dest_endCol = int(self.ui.lineEdit_DestSheetEndCol.text())

            """ 根据代码，客户ID， 填充汇总列 """
            exl = ExcelFillColumn()
            exl.fill_all_sum(
                dest_filename=dest_filename,
                dest_sheetname=dest_sheetname,
                dest_sum_row=dest_sumRow,
                dest_price_col=dest_priceCol,
                dest_start_row=dest_startRow,
                dest_end_row=dest_endRow,
                dest_start_col=dest_startCol,
                dest_end_col=dest_endCol,
            )

            QMessageBox.about(self, R.AppName, "填充列成功！")

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")

    def FillColumn(self):
        self.setWindowTitle("正在运行填充单列，请等待...")
        try:
            org_filename = self.ui.lineEdit_OrgFilename.text().strip()
            org_sheetname = self.ui.lineEdit_OrgSheetName.text().strip()
            # org_clientId = int(self.ui.lineEdit_OrgSheetClientId.text())
            org_codeCol = R.toInt(self.ui.lineEdit_OrgSheetCodeCol.text())
            org_startRow = R.toInt(self.ui.lineEdit_OrgSheetStartRow.text())
            org_endRow = R.toInt(self.ui.lineEdit_OrgSheetEndRow.text())
            org_startCol = R.toInt(self.ui.lineEdit_OrgSheetStartCol.text())
            # org_endCol = R.toInt(self.ui.lineEdit_OrgSheetEndCol.text())
            dest_filename = self.ui.lineEdit_DestFilename.text().strip()
            dest_sheetname = self.ui.lineEdit_DestSheetName.text().strip()
            dest_codeCol = R.toInt(self.ui.lineEdit_DestSheetCodeCol.text())
            dest_startRow = R.toInt(self.ui.lineEdit_DestSheetStartRow.text())
            dest_endRow = R.toInt(self.ui.lineEdit_DestSheetEndRow.text())
            dest_startCol = R.toInt(self.ui.lineEdit_DestSheetStartCol.text())
            dest_isSum = self.ui.checkBox_IsSum.isChecked()
            is_sorted = self.ui.checkBox_isSort.isChecked()

            """ 根据代码，客户ID， 填充汇总列 """
            exl = ExcelFillColumn()
            exl.fill_col(
                src_filename=org_filename,
                src_sheetname=org_sheetname,
                src_code_col=org_codeCol,
                src_start_row=org_startRow,
                src_end_row=org_endRow,
                src_fill_col=org_startCol,
                dest_filename=dest_filename,
                dest_sheetname=dest_sheetname,
                dest_code_col=dest_codeCol,
                dest_start_row=dest_startRow,
                dest_end_row=dest_endRow,
                dest_fill_col=dest_startCol,
                is_sum=dest_isSum,
                is_sort = is_sorted
            )

            QMessageBox.about(self, R.AppName, "填充列成功！")

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")
        self.setWindowTitle(R.AppName)

    def FillColumnsHZ(self):
        self.setWindowTitle("正在运行填充汇总，请等待...")
        self.FillColumns(is_hz=True)
        self.setWindowTitle(R.AppName)

    def FillColumnsXT(self):
        self.setWindowTitle("正在运行填充系统，请等待...")
        self.FillColumns(is_hz=False)
        self.setWindowTitle(R.AppName)
